package controllers

import (
	"fly/helper"
	"fly/models"
	"fly/req"
	"fly/resp"
	"fly/service"
	"github.com/beego/beego/v2/client/orm"
	"github.com/duke-git/lancet/v2/convertor"
)

// SysRoleController 系统角色
type SysRoleController struct {
	BaseController
	SysDeptService     service.SysDeptService
	SysRoleService     service.SysRoleService
	SysRoleMenuService service.SysRoleMenuService
	SysRoleDataService service.SysRoleDataService
	SysUserService     service.SysUserService
}

// AddSysRole ...
// @Title 添加角色
// @Description 添加一个系统配置-角色
// @Param token header  string	false "授权Token"
// @Param body body req.AddRoleReq true "添加角色数据"
// @Success 200 {object} resp.SysRoleResp
// @router /save [post]
func (c *SysRoleController) AddSysRole() {
	con := req.AddRoleReq{}
	c.CheckReqErrorAbort(c.BindJSON(&con))
	c.ValidReqParams(&con)
	mSysRole := models.SysRole{}
	c.CheckRespErrorAbort(helper.CopyDeep(&mSysRole, con))
	id := con.Id
	var err error
	if con.Id > 0 {
		old, _ := models.GetSysRoleById(con.Id)
		c.CheckReqErrorAbort(helper.RequireTrue(old != nil, "未找到资源"))
		_ = helper.CopyOut(old, con)
		err = models.UpdateSysRoleById(old, c.GetReqUserId())
	} else {
		id, err = models.AddSysRole(&mSysRole, c.GetReqUserId())
	}
	c.CheckRespErrorAbort(err)
	// 保存角色菜单关系
	err = c.SysRoleMenuService.SaveOrUpdate(id, con.MenuIdList, c.GetReqUserId())
	// 保存角色数据权限关系
	err = c.SysRoleDataService.SaveOrUpdate(id, con.DeptIdList, c.GetReqUserId())
	c.CheckRespErrorAbort(err)
	c.Ok()
}

// All ...
// @Title 查询角色
// @Description 查询所有角色列表
// @Param token header  string	false "授权Token"
// @Success 200 {object} []resp.SysRoleResp
// @router /list [get]
func (c *SysRoleController) All() {
	//user, err := models.GetSysUserById(c.GetReqUserId())
	//subIds, err := c.SysDeptService.GetSubDeptIdListByUserId(c.GetReqUserId())
	//c.CheckRespErrorAbort(err)
	list, err := c.SysRoleService.GetByRoleIds()
	c.CheckRespErrorAbort(err)
	var results []resp.SysRoleResp
	c.CheckRespErrorAbort(resp.Copy2TimeResp(&results, &list))
	c.Success(results)
}

// Detail ...
// @Title 角色详情
// @Description 通过id查询角色详情
// @Param token header  string	false "授权Token"
// @Param path id int true "角色id"
// @Success 200 {object} []resp.SysRoleResp
// @router /:id [get]
func (c *SysRoleController) Detail() {
	id, err := convertor.ToInt(c.Ctx.Input.Param(":id"))
	c.CheckReqErrorAbort(err)
	role, err := models.GetSysRoleById(id)
	c.CheckRespErrorAbort(err)
	respRole := resp.SysRoleResp{}
	_ = resp.Copy2TimeResp(&respRole, role)
	// 查询角色对应的菜单
	menuList, _ := c.SysRoleMenuService.GetMenuIdList(id)
	respRole.MenuIdList = menuList
	//查询角色对应的数据权限
	deptList, _ := c.SysRoleDataService.GetDeptIdList(id)
	respRole.DeptIdList = deptList
	c.Success(respRole)
}

// Del ...
// @Title 删除角色
// @Description 查询所有角色列表
// @Param token header  string	false "授权Token"
// @Param path id int true "角色id"
// @Success 200 {int} "id"
// @router /:id [delete]
func (c *SysRoleController) Del() {
	id, err := convertor.ToInt(c.Ctx.Input.Param(":id"))
	c.CheckReqErrorAbort(err)
	c.CheckReqErrorAbort(helper.RequireTrue(c.SysUserService.IsSuperAdminUser(c.GetReqUserId()), "超级管理员才能操作"))
	c.CheckRespErrorAbort(models.DeleteSysRole(id))
	// 删除角色用户关系
	_, err = orm.NewOrm().QueryTable(new(models.SysRoleUser)).Filter("role_id", id).Delete()
	//删除角色菜单关系
	_, err = orm.NewOrm().QueryTable(new(models.SysRoleMenu)).Filter("role_id", id).Delete()
	//删除角色数据权限关系
	_, err = orm.NewOrm().QueryTable(new(models.SysRoleData)).Filter("role_id", id).Delete()
	c.Success(id)
}
